package com.shark.security.jwt.persistent.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.shark.security.jwt.persistent.entity.RolePermissionEntity;

/**
 * 角色、权限关联
 * @author junxiao
 *
 */
@Repository
public class RolePermissionRepository {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTemplate;
	
	private static final String SQL_GET_PERMISSION_BY_ROLE = "select distinct role_id, permission_id from t_sys_role_permission where role_id in (:lisRoleId)";
	
	public List<RolePermissionEntity> getPermissonByRole(List<Integer> lisRoleId) {
		Map<String, Object> mapParam = new HashMap<>();
		mapParam.put("lisRoleId", lisRoleId);
		return jdbcTemplate.query(SQL_GET_PERMISSION_BY_ROLE, mapParam, new RowMapper<RolePermissionEntity>() {

			@Override
			public RolePermissionEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
				RolePermissionEntity u = new RolePermissionEntity();
				u.setRoleId(rs.getInt("role_id"));
				u.setPermissionId(rs.getInt("permission_id"));
				return u;
			}
		
		});
	}
}
